'use strict';

const base = require('../base/controllerBase');

module.exports = class extends base {
    /**
     * 注册用户
     * @returns {Promise<void>}
     */
    async register() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                userType: {type: 'string'},
                username: {type: 'string'},
                password: {type: 'string'},
                tenantCode: {type: 'string'}
            };
            ctx.validate(model, body);
            this.app.logger.info('user register', body);
            const res = await service.userService.register(body);
            ctx.body = res;
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            this.app.logger.error('user register', _message, e);
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async login() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {username: {type: 'string'}, password: {type: 'string'}};

            ctx.validate(model, body);

            const res = await service.userService.login(body);
            body.action = 'login';
            body.status = res.status;
            const tc = res.result ? res.result.tenantCode : undefined;
            body.tenantCode = tc;

            ctx.body = res;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findUser() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;

            const model = {username: {type: 'string'}, tenantCode: {type: 'string'}};
            ctx.validate(model, body);
            const result = await ctx.model.User.findOne(body, null, {lean: true});
            result && delete result['password'];

            ctx.body = {status: 'ok', result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findUsers() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {tenantCode: {type: 'string'}};
            ctx.validate(model, body);
            const result = await ctx.app.dbs.find('User', body);
            result && delete result['password'];

            ctx.body = {status: 'ok', result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findUsersByPage() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                tenantCode: {type: 'string'},
                skip: {type: 'number'},
                limit: {type: 'number'},
            };
            ctx.validate(model, body);
            const result = await service.userService.findUsersByPage(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async updateUser() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {username: {type: 'string'}, tenantCode: {type: 'string'}};
            ctx.validate(model, body);
            const res = await service.userService.updateUser(body);
            ctx.body = res;
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async deleteUser() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {userCode: {type: 'string'}, tenantCode: {type: 'string'}};
            ctx.validate(model, body);
            const res = await service.userService.deleteUser(body);
            ctx.body = res;
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

};
